<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>中转发信息打印</title>
		<script type="text/javascript">
		var LODOP=null;

	//选中订单
	function alreadyMyChecked(item) {
		if ($("#check_already" + item).is(":checked")) {

		} else {
			$("#alreadyMyCheckedAll").attr("checked", false);
		}
	}
	//全选
	function alreadyMyCheckedAll() {
		$("input[name='alreadyMyChecked']").attr("checked",
				$("#alreadyMyCheckedAll").is(":checked"));
	}
	
	//查找选中的订单
	function alreadyMyCheckedAllTWS() {
		var tempChecked = "";
		$("input[type='checkbox'][name='alreadyMyChecked']:checked").each(function() {
		     if(tempChecked==""){
		        tempChecked=$(this).val();
		     }else{
		       tempChecked = tempChecked + ":" + $(this).val();
		     }
			

		});
		return tempChecked;
	}
	
	
	function initPrinter(item, arg1) {
		backupTaskCode = item;
		username = arg1;
		var indexLength = getPrinterCount();
		var tempString = "";
		for ( var i = 0; i < indexLength; i++) {
			var name = getPrinterName(i);
			tempString += "<option value='"+name+"'>" + name + "</option>";
		}
		$("#printerBody").html(tempString);
	}

	//获得打印机的个数
	function getPrinterCount() {
		LODOP = getLodop(document.getElementById('LODOP_OB'), document
				.getElementById('LODOP_EM'));
		return LODOP.GET_PRINTER_COUNT();
	};

	//根据打印机序号获得打印机名称
	function getPrinterName(iPrinterNO) {
		LODOP = getLodop(document.getElementById('LODOP_OB'), document
				.getElementById('LODOP_EM'));
		return LODOP.GET_PRINTER_NAME(iPrinterNO);
	};
	
	;(function($) {
    $.ajaxQueue = {
        requests: new Array(),
        offer: function(options) {
            var _self = this,
               xhrOptions = $.extend({}, options, {
                   complete: function(jqXHR, textStatus) {
                        if(options.complete)
                            options.complete.call(this, jqXHR, textStatus);
                            _self.poll();
                   },
                   beforeSend: function(jqXHR, settings) {
                       if(options.beforeSend)
                           var ret = options.beforeSend.call(this, jqXHR, settings);
                       if(ret === false) { 
                           _self.poll();
                           return ret;
                       }
                   }
               });
            this.requests.push(xhrOptions);
            if(this.requests.length == 1) {
                $.ajax(xhrOptions);
            }
        },
        poll: function() {
            if(this.isEmpty()) {
                return null;    
            }
            var processedRequest = this.requests.shift();
            var nextRequest = this.peek();
            if(nextRequest != null) {
                $.ajax(nextRequest);
            }
            return processedRequest;
        },
        peek: function() {
            if(this.isEmpty()) {
                return null;
            }
            var nextRequest = this.requests[0];
            return nextRequest;
        },
        isEmpty: function() {
            return this.requests.length == 0;
        }
    };
})(jQuery);

//整除
function Div(exp1, exp2){
    var n1 = Math.round(exp1); //四舍五入
    var n2 = Math.round(exp2); //四舍五入
    var rslt = n1 / n2; //除
    if (rslt >= 0){
        rslt = Math.floor(rslt); //返回值为小于等于其数值参数的最大整数值。
    }else {
        rslt = Math.ceil(rslt); //返回值为大于等于其数字参数的最小整数。
    }
    return rslt;
}
	
function pageAjaxPrinter(){
      var orders=alreadyMyCheckedAllTWS();
      var printerName=$("#printerBody").val();
	  if(orders==""){
	    $("#message").html("请选择订单");
	    $("#message").show();
	    hideMessage();
	    return;
	   }
	 var pageCountNumber=60;
	 var orderLists=orders.split(":");
	 var length=orderLists.length;
	 var div=Div(length,pageCountNumber);
	 var pageCount=length%pageCountNumber==0? div:div+1;
	 showBg();
	 var end=0;
	 for(var i=0;i<pageCount;i++){
	    if(i==pageCount-1){
	      end=1;
	    }
	    var tempOrders="";
	    var temp=i*pageCountNumber;
	    var indexTempLength=(i+1)*pageCountNumber;
	    for(var j=temp;j<length&&j<indexTempLength;j++){
	       if(""==tempOrders){
	          tempOrders=orderLists[j];
	       }else{
	          tempOrders+=":"+orderLists[j];
	       }
	    }
	       if(""==tempOrders){
	            continue;
	        }
	   $.ajaxQueue.offer({
            type : 'post',
			url : '${ctx}/printBackupTask/ajaxPrintOrdeno',
			data : 'ordenos='+tempOrders+'&end='+end+'&backupTaskCode=${obj.backupTaskCode}',
			dataType : 'json',
			success : function(data) {
				
			},
        beforeSend: function(jqXHR, settings) {
        
        },
        success: function(data, textStatus, jqXHR) {
           if (data.status == "1") {
                var waybillOne="";
                var waybillTwo="";
                var waybillThree="";
                var waybillFour="";
                var table="<table border='0' width='210mm' height='297mm'><tr><td  width='50%' height='50%'>";
                var count=data.content.length;
				$.each(data.content, function(i) {
				   
			   var tableString="<table  border='1' style='width:100%; height:100%' ><tr style='height:17mm;'  ><td><div style='margin:13mm 5px 0mm 0mm;'>";
                   //网点信息  网点名称 省份名称
                   tableString+="<font size='6'>"+data.content[i].areaGroupName+"</font>";
                   tableString+="<font size='3' style='margin-left:1mm;'>("+data.content[i].provna+")</font>";
                   tableString+="<font size='5' style='margin-left:1mm;'>"+data.content[i].customerName+"</font></div>";
                   tableString+="</td></tr><tr><td valign='top'>";
                   //商品信息
                   tableString+="<style>table{ border-collapse:collapse; border:1px solid #ddd;}table tr td{ border:1px solid #ddd;}</style>";
                   tableString+= "<table border='1' style='font-size:8px;height: auto;' align='center'><tr><th>柜位</th><th>编码</th><th>数量</th></tr>";

                       $.each(data.content[i].printEdiOrdenoItem, function(j) {
                          tableString+="<tr><td>"+data.content[i].printEdiOrdenoItem[j].storeCode+"</td><td>"+data.content[i].printEdiOrdenoItem[j].sku+"</td><td>"+data.content[i].printEdiOrdenoItem[j].qty+"</td></tr>";			
                        });
                  
  
                    tableString+= "</table>";
                    //收件人信息
                    tableString+= "</td></tr><tr  style='height:1mm;'><td>";
                    tableString+="<font size='3'>"+data.content[i].recena+"<font><div style='font-size:8px;'>";
                    tableString+=data.content[i].mobile+"  "+data.content[i].teleph+" <br>";
                    tableString+=data.content[i].addres;
                    tableString+="</div></td></tr></table>";
                    if(i%4==0){
                      table+=tableString+"</td><td  width='50%' height='50%'>";
                      waybillOne=data.content[i].waybillNo;
                      if(i==(count-1)){
                          waybillTwo="";
                          waybillThree="";
                          waybillFour="";
                          table+="</td></tr><tr><td  width='50%' height='50%'></td><td width='50%' height='50%'></td></tr></table>";
                        printPackStyle(data.content[i].ordeno,printerName,table,waybillOne,waybillTwo,waybillThree,waybillFour,i+1);
                      }
                    }else if(i%4==1){
                      table+=tableString+"</td></tr><tr><td  width='50%' height='50%'>";
                       waybillTwo=data.content[i].waybillNo;
                       if(i==(count-1)){
                          waybillThree="";
                          waybillFour="";
                          table+="</td><td width='50%' height='50%'></td></tr></table>";
                        printPackStyle(data.content[i].ordeno,printerName,table,waybillOne,waybillTwo,waybillThree,waybillFour,i+1);
                      }
                    }else if(i%4==2){
                      table+=tableString+"</td><td width='50%' height='50%'>";
                       waybillThree=data.content[i].waybillNo;
                       if(i==(count-1)){
                         waybillFour="";
                         table+="</td></tr></table>";
                        printPackStyle(data.content[i].ordeno,printerName,table,waybillOne,waybillTwo,waybillThree,waybillFour,i+1);
                      }
                    }else if(i%4==3){
                      table+=tableString+"</td></tr></table>";
                     // alert(table);
                       waybillFour=data.content[i].waybillNo;
                      printPackStyle(data.content[i].ordeno,printerName,table,waybillOne,waybillTwo,waybillThree,waybillFour,i+1);
                      table="<table border='0' width='210mm' height='297mm'><tr><td  width='50%' height='50%'>";
                    }
				});
				if(data.end==1){
				  closeBg();
				  $("#message").html("<span class=\"label label-success\">打印机加载完毕</span>");
				}
			} else {
				closeBg();
				alert(data.content);
		    }
         }
       });
	 }
}


function pageAjaxPrinterSingle(){
      var orders=alreadyMyCheckedAllTWS();
      var printerName=$("#printerBody").val();
	  if(orders==""){
	    $("#message").html("请选择订单");
	    $("#message").show();
	    hideMessage();
	    return;
	   }
	 var pageCountNumber=60;
	 var orderLists=orders.split(":");
	 var length=orderLists.length;
	 var div=Div(length,pageCountNumber);
	 var pageCount=length%pageCountNumber==0? div:div+1;
	 showBg();
	 var end=0;
	 for(var i=0;i<pageCount;i++){
	    if(i==pageCount-1){
	      end=1;
	    }
	    var tempOrders="";
	    var temp=i*pageCountNumber;
	    var indexTempLength=(i+1)*pageCountNumber;
	    for(var j=temp;j<length&&j<indexTempLength;j++){
	       if(""==tempOrders){
	          tempOrders=orderLists[j];
	       }else{
	          tempOrders+=":"+orderLists[j];
	       }
	    }
	       if(""==tempOrders){
	            continue;
	        }
	   $.ajaxQueue.offer({
            type : 'post',
			url : '${ctx}/printBackupTask/ajaxPrintOrdeno',
			data : 'ordenos='+tempOrders+'&end='+end+'&backupTaskCode=${obj.backupTaskCode}',
			dataType : 'json',
			success : function(data) {
				
			},
        beforeSend: function(jqXHR, settings) {
        
        },
        success: function(data, textStatus, jqXHR) {
           if (data.status == "1") {
           
				$.each(data.content, function(i) {
				   
				   var tableString="<div><h4>物流单</h4></div><table width='100%'  height='100px' style='border:1px solid #333333' cellpadding='1'>";
				   tableString+=" <tr style='font-size:20px'><td style='font-weight:bold'>"+data.content[i].areaGroupName+"</td>";
				   tableString+="<td>"+data.content[i].provna+"</td>";
				   tableString+="<td>"+data.content[i].customerName+"</td>";
				   tableString+="</tr><tr style='font-size:16px'>";
				   tableString+="<td>"+data.content[i].recena+"("+data.content[i].mobile+")</td>";
				   tableString+="<td>订单号："+data.content[i].ordeno+"</td>";
				   tableString+="<td>运单号："+data.content[i].waybillNo+"</td></tr>";
				   tableString+="<tr style='font-size:12px'><td colspan='3'>"+data.content[i].addres+"</tr>";
				   tableString+="<tr style='font-size:20px'><td colspan='3'>"+data.content[i].message+"</tr>";
				   tableString+="</table><div><h4>分拣单</h4></div>";
				   tableString+="<table width='80%' style='font-size:13px;border:1px solid #333333;'>";
				   tableString+="<tr style='background-color:#333333; color:#FFFFFF'>";
				   tableString+="<td>库位</td><td>商品</td><td>数量</td></tr>";
				   $.each(data.content[i].printEdiOrdenoItem, function(j) {
				     tableString+="<tr>";
				     tableString+="<td>"+data.content[i].printEdiOrdenoItem[j].storeCode+"</td>";
				     tableString+="<td>"+data.content[i].printEdiOrdenoItem[j].sku+"</td>";
				     tableString+="<td>"+data.content[i].printEdiOrdenoItem[j].qty+"</td>";
				     tableString+="</tr>";
				   });
				   tableString+="</table>";
				   
				   
				   
               
                    printPackStyleSingle(data.content[i].ordeno,printerName,tableString,data.content[i].waybillNo,i+1);
				});
				if(data.end==1){
				  closeBg();
				  $("#message").html("<span class=\"label label-success\">打印机加载完毕</span>");
				}
			} else {
				closeBg();
				alert(data.content);
		    }
         }
       });
	 }
}


function printPackStyleSingle(taskName, printerName, printContent, waybillNo,rankno) {
		LODOP.PRINT_INIT(taskName);
		LODOP.ADD_PRINT_TEXT(30,10,2,2,rankno);		
		
      
		//LODOP.ADD_PRINT_BARCODE(15,100,200,40,"128B",waybillNo);
		LODOP.ADD_PRINT_HTM(0, 0, "100%", "100%",  printContent);
	    LODOP.ADD_PRINT_BARCODE(220,460,50,300,"128B",waybillNo);
	    LODOP.SET_PRINT_STYLEA(0,"Angle",90);
		LODOP.ADD_PRINT_BARCODE(700,427,150,40,"128A",waybillNo);
		if (LODOP.SET_PRINTER_INDEXA(printerName)) {
		   LODOP.PRINT();
		   //LODOP.PRINT_DESIGN();
		}
	}


function printPackStyle(taskName, printerName, printContent, waybillOne,waybillTwo,waybillThree,waybillFour,rankno) {
		LODOP.PRINT_INIT(taskName);
		LODOP.ADD_PRINT_TEXT(2,2,30,10,rankno);		
		
		var strCenterStyle = "<style/>table {width:100%;height:100%}</style>";
		LODOP.ADD_PRINT_HTM(0, 0, "100%", "100%",  strCenterStyle+printContent);
		if(waybillOne!=""){
		  LODOP.ADD_PRINT_BARCODE(25,20,350,40,"128B",waybillOne);
		}
	   if(waybillTwo!=""){
	    LODOP.ADD_PRINT_BARCODE(25,410,350,40,"128B",waybillTwo);
	     
		}
	   if(waybillThree!=""){
	    LODOP.ADD_PRINT_BARCODE(555,20,350,40,"128B",waybillThree);
		}
		if(waybillFour!=""){
		 LODOP.ADD_PRINT_BARCODE(555,410,350,40,"128B",waybillFour );
		}
		if (LODOP.SET_PRINTER_INDEXA(printerName)) {
			LODOP.PRINT();
			//LODOP.PRINT_DESIGN();
		}
	}
	
</script>

	</head>

	<body>
		<div id="message" class="alert alert-success" style="display: none;">
			<span id="content"></span>
		</div>
		<div id="fullbg"></div>
		<div id="dialog">
			<div>
				<img src="${ctx }/static/022.gif"></img>
			</div>
		</div>
		<table id="contentTable"
			class="table table-striped table-bordered table-condensed" >
			<thead>
				<tr>
					<th>
						编号
					</th>
					<th>
						生成日期
					</th>
					<th>
						处理日期
					</th>
					<th>
						订单数量
					</th>
					<th>
						商品数量
					</th>
					<th>
						商品种类
					</th>
					<th>
						类别
					</th>
					<th>
						操作人
					</th>

				</tr>
			</thead>
			<tbody>
				<tr>
					<td>
						${obj.backupTaskCode }
					</td>
					<td>
						${obj.createDate }
					</td>
					<td>
						${obj.updateDate }
					</td>
					<td>
						${obj.ordenoQty }
					</td>
					<td>
						${obj.skuQty }
					</td>
					<td>
						${obj.skuType }
					</td>
					<td>
						<c:if test="${obj.type eq 'single' }">
							         单件
							</c:if>
						<c:if test="${obj.type eq 'multiple' }">
							            多件
							</c:if>
						<c:if test="${obj.type eq 'forward' }">
							       中转发
							</c:if>
					</td>
					<td>
						${obj.username }
					</td>
				</tr>
			</tbody>
		</table>
<div><a href="#selextPrinter" role="button" data-toggle="modal"
									class="btn btn-primary"
									onclick="initPrinter('${obj.backupTaskCode}')">开始打印</a></div>
		<table class="table table-striped table-bordered table-condensed">
			<thead>
				<tr>
				  <th style="width: 50px;">
									全选:
									<input type="checkbox" name="alreadyMyCheckedAll"
										id="alreadyMyCheckedAll" onclick="alreadyMyCheckedAll()" />
					<th>
						订单号
					</th>

					<th>
						收货人
					</th>
					<th>
						收货地址
					</th>
					<th>
						手机
					</th>
					<th>
						查询
					</th>
				</tr>
			</thead>
			<tbody>
				<c:forEach items="${ediOrders}" var="order" step="1" varStatus="i">
					<tr>
					   <td align="right">
										<input type="checkbox" name="alreadyMyChecked"
											id="check_already${i.index+1}"
											value="${order.ordeno}"
											onclick="alreadyMyChecked(${order.id})" />
						<td>
							${order.ordeno}
						</td>
						<td>
							${order.recena}
						</td>
						<td>
							${order.addres}
						</td>
						<td>
							${order.mobile}
						</td>
						<td>
							<a href="#ordersDetailed"
								onclick="getAjaxOrders('${order.id}')" role="button"
								class="btn" data-toggle="modal"> 订单详情 </a>

							<a href="#itemsDetailed"
								onclick="getAjaxItems('${order.id}')" role="button"
								class="btn" data-toggle="modal"> 商品详情 </a>
						</td>
					</tr>
				</c:forEach>
			</tbody>
		</table>

		<div style="display: none; width: 1000" class="modal hide fade"
			tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
			aria-hidden="true" id="ordersDetailed">
			<div class="modal-header">
				<h3 id="myModalLabel">
					订单详细信息
				</h3>
			</div>
			<div class="modal-body">
				<table class="table table-striped table-bordered table-condensed"
					id="modalBodyOrders"></table>
			</div>
			<div class="modal-footer">
				<button class="btn" data-dismiss="modal" aria-hidden="true">
					关闭
				</button>
			</div>
		</div>

		<div style="display: none; width: 1000" class="modal hide fade"
			tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
			aria-hidden="true" id="itemsDetailed">
			<div class="modal-header">
				<h3 id="myModalLabel">
					商品详细信息
				</h3>
			</div>
			<div class="modal-body">
				<table class="table table-striped table-bordered table-condensed"
					id="modalBodyItems"></table>
			</div>
			<div class="modal-footer">
				<button class="btn" data-dismiss="modal" aria-hidden="true">
					关闭
				</button>
			</div>
		</div>
		
		<!-- 设置打印机 -->
			<div style="display: none; width: 1000" class="modal hide fade"
			tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
			aria-hidden="true" id="selextPrinter">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"
					aria-hidden="true">
					×
				</button>
				<h3 id="printerNumber">
					选择打印机
				</h3>
			</div>
			<div class="modal-body">
				<select id="printerBody" name="printerBody">
				</select>
			</div>
			<div class="modal-footer">
				<button class="btn" data-dismiss="modal" aria-hidden="true"  onclick="pageAjaxPrinterSingle();">
									开始打印
								</button>
				<button class="btn" data-dismiss="modal" aria-hidden="true">
					Close
				</button>
			</div>
		</div>
	</body>
</html>
